/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.bean;

import org.ipti.br.tag.bo.CampaignBO;
import org.ipti.br.tag.bo.PersonageBO;
import org.ipti.br.tag.bo.UnityBO;
import org.ipti.br.tag.entity.Campaign;
import org.ipti.br.tag.entity.Human;
import org.ipti.br.tag.entity.Unity;
import org.ipti.br.tag.entity.enums.CampaignType;
import org.ipti.br.tag.entity.enums.Sex;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;
import org.primefaces.model.chart.PieChartModel;
import org.ipti.br.tag.util.HbUtil;

/*
 *
 * @author thor
 */
@Named(value="home")
@SessionScoped
public class HomeBean extends AppBaseBean {

    @Inject private SessionBean sessionBean;
    @Inject private PersonageBO personageBO;
    @Inject private UnityBO unityBO;
    @Inject private CampaignBO campaignBO;
    
    private double imc;
    private int qtdDesnutridos, qtdSobrepesos, qtdAnemicos;
    private PieChartModel pieModelWH, pieModelHb;

    public HomeBean() {
        pieModelWH = new PieChartModel();
        pieModelHb = new PieChartModel();
    }

    public void setValuesInCharmodelWH(int desnutridos, int sobrepesos, int normais, int total) {
        pieModelWH.set("Desnutridos", desnutridos);
        pieModelWH.set("Sobrepesos", sobrepesos);
        pieModelWH.set("Normais", normais);
        pieModelWH.set("Total", total - (desnutridos + sobrepesos + normais));
    }

    public void setValuesInCharmodelHb(int anemicos, int naoAnemicos, int total) {

        pieModelHb.set("Anemicos", anemicos);
        pieModelHb.set("Não Anemicos", naoAnemicos);
        pieModelHb.set("Não Participaram", total);

    }

    @PostConstruct
    public void init() {
        loadWeightAndHeightInfo();
        loadAnemicInfo();
        loadWarnings();
    }
    
        int qtdTotalAlunosDaEscola = 0;
        
    public void loadAnemicInfo() {
        Campaign currentCampaign = campaignBO.findCurrentCampaign(CampaignType.ANEMIA_FERROPRIVA);
        int qtdAnemicos = 0;
        int qtdNaoAnemicos = 0;
        //int qtdTotalAlunosDaEscola = 0;

//        if (isLoggedinSecretary()) {
//            List<Unity> unities = unityBO.getUnityChildren(sessionBean.getLoggedActor().getUnity().getId());
//            if (unities != null) {
//                for (Unity elementUnity : unities) {
//                    Unity school = unityBO.getSchool(elementUnity);
//                    if (school != null) {
//                        qtdTotalAlunosDaEscola += (unityBO.getStudentsBySchool(school)).size();
//                        List<Object[]> humanandHb = campaignBO.getCampaignListHb(school, currentCampaign);
//                        if (humanandHb != null) {
//                            for (int i = 0; i < humanandHb.size(); i++) {
//                                if (!((Human) humanandHb.get(i)[0]).equals(((Human) humanandHb.get(i)[0]))) {
//                                    int months = HbUtil.monthsBetweenDates(((Human) humanandHb.get(i)[0]).getBirthday(), Calendar.getInstance());
//
//                                    if (HbUtil.avaliate(months, (Float) humanandHb.get(i)[1], (Human) humanandHb.get(i)[0])) {
//                                        qtdAnemicos++;
//                                    } else {
//                                        qtdNaoAnemicos++;
//                                    }
//                                }
//                            }
//                        }
//                    }
//                }
//            }
//
//        } else {
//
//            Unity school = unityBO.getSchool(sessionBean.getLoggedActor().getUnity());
//            if (school != null) {
//                qtdTotalAlunosDaEscola += (unityBO.getStudentsBySchool(school)).size();
//                List<Object[]> humanandHb = campaignBO.getCampaignListHb(school, currentCampaign);
//                for (int i = 0; i < humanandHb.size(); i++) {
//                    if (!((Human) humanandHb.get(i)[0]).equals(((Human) humanandHb.get(i)[0]))) {
//                        int months = HbUtil.monthsBetweenDates(((Human) humanandHb.get(i)[0]).getBirthday(),Calendar.getInstance());
//
//                        if (HbUtil.avaliate(months, (Float) humanandHb.get(i)[1], (Human) humanandHb.get(i)[0])) {
//                            qtdAnemicos++;
//                        } else {
//                            qtdNaoAnemicos++;
//                        }
//                    }
//                }
//            }
//        }

        this.qtdAnemicos = qtdAnemicos;
        setValuesInCharmodelHb(qtdAnemicos, qtdNaoAnemicos, qtdTotalAlunosDaEscola);
    }

    public int getQuantidadedePesoeAltura(Unity unity) {
        return 0;
    }

    public int getQuantidadedeHbs(Unity unity) {
        return 0;
    }

    public void loadWeightAndHeightInfo() {
        Campaign currentCampaign = campaignBO.findCurrentCampaign(CampaignType.ANEMIA_FERROPRIVA);
        int qtdOverWeights = 0;
        int sumOverWeightsValues = 0;
        int qtdD = 0;
        int qtdDValues = 0;
        int qtdNormais = 0;
        int qtdNormaisValues = 0;
        int qtdAlunosTotaldaEscola = 0;

//        if (isLoggedinSecretary()) {
//            List<Unity> unities = unityBO.getUnityChildren(sessionBean.getLoggedActor().getUnity().getId());
//            if (unities != null) {
//
//                for (Unity elementUnity : unities) {
//                    Unity school = unityBO.getSchool(elementUnity);
//                    if (school != null) {
//                        qtdAlunosTotaldaEscola += (unityBO.getStudentsBySchool(school)).size();
//                        List<Object[]> humanandWH = campaignBO.getCampaignListWeigthAndHeight(school, currentCampaign);
//                        if (humanandWH != null) {
//
//                            for (int i = 0; i < humanandWH.size(); i++) {
//                                float imc = calculateIMC(((WeightAndHeight) humanandWH.get(i)[1]).getWeight(),
//                                        ((WeightAndHeight) humanandWH.get(i)[1]).getHeight());
//
//                                int result = analyzeHuman((Human) humanandWH.get(i)[0], imc);
//
//                                if (result == 1) {
//                                    qtdD++;
//                                    qtdDValues += imc;
//                                } else if (result == 2) {
//                                    qtdOverWeights++;
//                                    sumOverWeightsValues += imc;
//                                } else {
//                                    qtdNormais++;
//                                    qtdNormaisValues += imc;
//                                }
//                            }
//                        }
//                    }
//
//                }
//            }
//
//        } else {
//            Unity school = unityBO.getSchool(sessionBean.getLoggedActor().getUnity());
//            if (school != null) {
//                qtdAlunosTotaldaEscola = (unityBO.getStudentsBySchool(school)).size();
//                List<Object[]> humanandWH = campaignBO.getCampaignListWeigthAndHeight(school, currentCampaign);
//                for (int i = 0; i < humanandWH.size(); i++) {
//                    float imc = calculateIMC(((WeightAndHeight) humanandWH.get(i)[1]).getWeight(),
//                            ((WeightAndHeight) humanandWH.get(i)[1]).getHeight());
//                    int result = analyzeHuman((Human) humanandWH.get(i)[0], imc);
//
//                    if (result == 1) {
//                        qtdD++;
//                        qtdDValues += imc;
//                    } else if (result == 2) {
//                        qtdOverWeights++;
//                        sumOverWeightsValues += imc;
//                    } else {
//                        qtdNormais++;
//                        qtdNormaisValues += imc;
//                    }
//                }
//            } else {
//                return;
//            }
//        }

//        if ((qtdD + qtdOverWeights + qtdNormais) != 0) {
//            this.imc = (qtdDValues + sumOverWeightsValues + qtdNormaisValues) / (qtdD + qtdOverWeights + qtdNormais);
//        }
        this.qtdDesnutridos = qtdD;
        this.qtdSobrepesos = qtdOverWeights;

        setValuesInCharmodelWH(qtdDesnutridos, qtdSobrepesos, qtdNormais, qtdAlunosTotaldaEscola);
    }

    public boolean isLoggedinSecretary() {
        if (sessionBean.getLoggedActor().getUnity().getOrganization().getId() == (long) 1049325440) {
            return true;
        } else {
            return false;
        }
    }

    public int analyzeHuman(Human human, float imc) {
        int age = HbUtil.monthsBetweenDates(human.getBirthday(), Calendar.getInstance());
        age = (int) Math.floor(age / 24);

        if (age <= 15) {
            return comparateAgeWithChildrenImcTable(human, imc, age);
        } else {
            return compareAdultImcTable(human, imc);
        }

    }

    public float calculateIMC(float weight, float height) {
        return (weight / (height * height));
    }

    public int comparateAgeWithChildrenImcTable(Human human, float measure, int age) {
        final int MALNOURISHED = 1; // Desnutrido
        final int OVERWEIGHT = 2; // Sobrepeso

        if (human.getSex() == Sex.MALE) {

            if ((age == 6) && (measure < 14.5)) {
                return MALNOURISHED;
            } else if ((age == 6) && (measure > 16.6)) {
                return OVERWEIGHT;
            } else if ((age == 7) && (measure < 15)) {
                return MALNOURISHED;
            } else if ((age == 7) && (measure > 17.3)) {
                return OVERWEIGHT;
            } else if ((age == 8) && (measure < 15.6)) {
                return MALNOURISHED;
            } else if ((age == 8) && (measure > 16.7)) { // Observação
                return OVERWEIGHT;
            } else if ((age == 9) && (measure < 16.1)) {
                return MALNOURISHED;
            } else if ((age == 9) && (measure > 18.8)) {
                return OVERWEIGHT;
            } else if ((age == 10) && (measure < 16.7)) {
                return MALNOURISHED;
            } else if ((age == 10) && (measure > 19.6)) {
                return OVERWEIGHT;
            } else if ((age == 11) && (measure < 17.2)) {
                return MALNOURISHED;
            } else if ((age == 11) && (measure > 20.3)) {
                return OVERWEIGHT;
            } else if ((age == 12) && (measure < 17.8)) {
                return MALNOURISHED;
            } else if ((age == 12) && (measure > 21.1)) {
                return OVERWEIGHT;
            } else if ((age == 13) && (measure < 18.5)) {
                return MALNOURISHED;
            } else if ((age == 13) && (measure > 21.9)) {
                return OVERWEIGHT;
            } else if ((age == 14) && (measure < 19.2)) {
                return MALNOURISHED;
            } else if ((age == 14) && (measure > 22.7)) {
                return OVERWEIGHT;
            } else if ((age == 15) && (measure < 19.9)) {
                return MALNOURISHED;
            } else if ((age == 15) && (measure > 23.6)) {
                return OVERWEIGHT;
            }

        } else if (human.getSex() == Sex.FEMALE) {

            if ((age == 6) && (measure < 14.3)) {
                return MALNOURISHED;
            } else if ((age == 6) && (measure > 16.1)) {
                return OVERWEIGHT;
            } else if ((age == 7) && (measure < 14.9)) {
                return MALNOURISHED;
            } else if ((age == 7) && (measure > 17.1)) {
                return OVERWEIGHT;
            } else if ((age == 8) && (measure < 15.6)) {
                return MALNOURISHED;
            } else if ((age == 8) && (measure > 18.1)) { // Observação
                return OVERWEIGHT;
            } else if ((age == 9) && (measure < 16.3)) {
                return MALNOURISHED;
            } else if ((age == 9) && (measure > 19.1)) {
                return OVERWEIGHT;
            } else if ((age == 10) && (measure < 17.0)) {
                return MALNOURISHED;
            } else if ((age == 10) && (measure > 20.1)) {
                return OVERWEIGHT;
            } else if ((age == 11) && (measure < 17.6)) {
                return MALNOURISHED;
            } else if ((age == 11) && (measure > 21.1)) {
                return OVERWEIGHT;
            } else if ((age == 12) && (measure < 18.3)) {
                return MALNOURISHED;
            } else if ((age == 12) && (measure > 22.1)) {
                return OVERWEIGHT;
            } else if ((age == 13) && (measure < 18.9)) {
                return MALNOURISHED;
            } else if ((age == 13) && (measure > 23.0)) {
                return OVERWEIGHT;
            } else if ((age == 14) && (measure < 19.3)) {
                return MALNOURISHED;
            } else if ((age == 14) && (measure > 23.8)) {
                return OVERWEIGHT;
            } else if ((age == 15) && (measure < 19.6)) {
                return MALNOURISHED;
            } else if ((age == 15) && (measure > 24.2)) {
                return OVERWEIGHT;
            }

        }

        return 0;
    }

    public int compareAdultImcTable(Human human, float measure) {
        final int MALNOURISHED = 1; // Desnutrido
        final int OVERWEIGHT = 2; // Sobrepeso

        if (measure < 18.5) {
            return MALNOURISHED;
        } else if (measure > 25) {
            return OVERWEIGHT;
        }

        return 0;
    }
    
    public void loadTotalChildrenPerUnity(){
        
    }
    
    public void loadWarnings() {
        System.out.println("Alertas carregados");
    }
    
    public List<String> getWarnings() {
        List<String> warnings = new ArrayList<String>();
        warnings.add("Anêmico sem medidas em 6 Ano A");
        warnings.add("Anêmico sem medidas em Ensino Infantil I");
        warnings.add("Anêmico sem medidas em EJA I");
        return warnings;
    }

    public double getImc() {
        return imc;
    }

    public void setImc(double imc) {
        this.imc = imc;
    }

    public int getQtdDesnutridos() {
        return qtdDesnutridos;
    }

    public void setQtdDesnutridos(int qtdDesnutridos) {
        this.qtdDesnutridos = qtdDesnutridos;
    }

    public int getQtdSobrepesos() {
        return qtdSobrepesos;
    }

    public void setQtdSobrepesos(int qtdSobrepesos) {
        this.qtdSobrepesos = qtdSobrepesos;
    }

    public PieChartModel getPieModelWH() {
        return pieModelWH;
    }

    public void setPieModelWH(PieChartModel pieModelWH) {
        this.pieModelWH = pieModelWH;
    }

    public PieChartModel getPieModelHb() {
        return pieModelHb;
    }

    public void setPieModelHb(PieChartModel pieModelHb) {
        this.pieModelHb = pieModelHb;
    }

    public int getQtdAnemicos() {
        return qtdAnemicos;
    }

    public void setQtdAnemicos(int qtdAnemicos) {
        this.qtdAnemicos = qtdAnemicos;
    }
    public int getqtdTotalAlunosDaEscola() {
        return qtdTotalAlunosDaEscola;
    }

    public void setqtdTotalAlunosDaEscola(int qtdTotalAlunosDaEscola) {
        this.qtdTotalAlunosDaEscola = qtdTotalAlunosDaEscola;
    }
}
